package com.ssy.lingxi.pay.serviceimpl;

import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.ssy.lingxi.pay.entity.AliPayNotifyRecord;
import com.ssy.lingxi.pay.entity.WeChatNotifyRecord;
import com.ssy.lingxi.pay.repository.AliPayNotifyRecordRepository;
import com.ssy.lingxi.pay.repository.WeChatNotifyRecordRepository;
import com.ssy.lingxi.pay.service.INotifyRecordService;
import com.ssy.lingxi.pay.service.IPayCacheService;
import jodd.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service
public class NotifyRecordServiceImpl implements INotifyRecordService {
    @Autowired
    private WeChatNotifyRecordRepository weChatNotifyRecordRepository;
    @Autowired
    private AliPayNotifyRecordRepository aliPayNotifyRecordRepository;
    @Autowired
    private IPayCacheService payCacheService;

    /**
     * 记录微信回调日志
     *
     * @param wxPayOrderNotifyResult 回调信息
     * @return 解析后的回调信息
     */
    @Override
    public WeChatNotifyRecord saveWeChatNotifyRecord(WxPayOrderNotifyResult wxPayOrderNotifyResult) {
        WeChatNotifyRecord weChatNotify = new WeChatNotifyRecord();
        BeanUtils.copyProperties(wxPayOrderNotifyResult, weChatNotify);
        return weChatNotifyRecordRepository.saveAndFlush(weChatNotify);
    }

    /**
     * 记录支付宝回调日志
     *
     * @param params 回调信息
     */
    @Override
    public AliPayNotifyRecord saveAliPayNotifyRecord(Map<String, String> params) {
        AliPayNotifyRecord aliPayNotifyRecord = new AliPayNotifyRecord();
        aliPayNotifyRecord.setAppId(params.get("app_id"));
        aliPayNotifyRecord.setBuyerId(params.get("buyer_id"));
        aliPayNotifyRecord.setAuthAppId(params.get("auth_app_id"));
        String buyerPayAmount = params.get("buyer_pay_amount");
        if (StringUtil.isNotEmpty(buyerPayAmount)) {
            aliPayNotifyRecord.setBuyerPayAmount(new Double(Double.parseDouble(buyerPayAmount) * 100).intValue());
        }
        String fundBullList = params.get("fund_bull_list");
        if (StringUtil.isNotEmpty(fundBullList)) {
            aliPayNotifyRecord.setFundBillList(payCacheService.serializeObject(fundBullList));
        }
        String invoiceAmount = params.get("invoice_amount");
        if (StringUtil.isNotEmpty(invoiceAmount)) {
            aliPayNotifyRecord.setInvoiceAmount(new Double(Double.parseDouble(invoiceAmount) * 100).intValue());
        }
        aliPayNotifyRecord.setNotifyId(params.get("notify_id"));
        aliPayNotifyRecord.setNotifyTime(params.get("notify_time"));
        aliPayNotifyRecord.setNotifyType(params.get("notify_type"));
        aliPayNotifyRecord.setOutTradeNo(params.get("out_trade_no"));
        String passbackParams = params.get("passback_params");
        if (StringUtil.isNotEmpty(passbackParams)) {
            aliPayNotifyRecord.setPassbackParams(payCacheService.serializeObject(passbackParams));
        }
        String body = params.get("body");
        if (StringUtil.isNotEmpty(body)) {
            aliPayNotifyRecord.setBody(payCacheService.serializeObject(body));
        }
        String pointAmount = params.get("point_amount");
        if (StringUtil.isNotEmpty(pointAmount)) {
            aliPayNotifyRecord.setPointAmount(new Double(Double.parseDouble(pointAmount) * 100).intValue());
        }
        String receiptAmount = params.get("receipt_amount");
        if (StringUtil.isNotEmpty(receiptAmount)) {
            aliPayNotifyRecord.setPointAmount(new Double(Double.parseDouble(receiptAmount) * 100).intValue());
        }
        aliPayNotifyRecord.setSellerId(params.get("seller_id"));
        aliPayNotifyRecord.setSubject(params.get("subject"));
        aliPayNotifyRecord.setTradeNo(params.get("trade_no"));
        aliPayNotifyRecord.setTradeStatus(params.get("trade_status"));
        String voucherDetailList = params.get("voucher_detail_list");
        if (StringUtil.isNotEmpty(voucherDetailList)) {
            aliPayNotifyRecord.setVoucherDetailList(payCacheService.serializeObject(voucherDetailList));
        }
        String sign = params.get("sign");
        if (StringUtil.isNotEmpty(sign)) {
            aliPayNotifyRecord.setSign(payCacheService.serializeObject(sign));
        }
        aliPayNotifyRecord.setGmtCreate(params.get("gmt_create"));
        aliPayNotifyRecord.setGmtPayment(params.get("gmt_payment"));
        return aliPayNotifyRecordRepository.saveAndFlush(aliPayNotifyRecord);
    }
}
